/*
 * FreeModbus Libary: BARE Port
 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id$
 */

#ifndef _PORT_H
#define _PORT_H

#include "intrinsics.h"
#include <assert.h>
#include <inttypes.h>

// 增加STM32的头文件
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "misc.h"

#define	INLINE                      inline
#define PR_BEGIN_EXTERN_C           extern "C" {
#define	PR_END_EXTERN_C             }

#define ENTER_CRITICAL_SECTION( )   __disable_interrupt();
#define EXIT_CRITICAL_SECTION( )    __enable_interrupt();

typedef uint8_t BOOL;

typedef unsigned char UCHAR;
typedef char CHAR;

typedef uint16_t USHORT;
typedef int16_t SHORT;

typedef uint32_t ULONG;
typedef int32_t LONG;

#ifndef TRUE
#define TRUE            1
#endif

#ifndef FALSE
#define FALSE           0
#endif

// 添加 modbus 串口和定时器的宏定义
// Modbus串口  
#define USE_USART_NUM               1       // 使用的串口号

#if (USE_USART_NUM == 1)        // 使用串口1
    #define MODBUS_USART                USART1  
    #define MODBUS_USART_CLK            RCC_APB2Periph_USART1
    #define MODBUS_USART_CLK_CMD        RCC_APB2PeriphClockCmd
    #define MODBUS_USART_GPIO_CLK       RCC_APB2Periph_GPIOA
    #define MODBUS_USART_GPIO_CLK_CMD   RCC_APB2PeriphClockCmd
    #define MODBUS_USART_TX_PORT        GPIOA  
    #define MODBUS_USART_RX_PORT        GPIOA  
    #define MODBUS_USART_TX_PIN         GPIO_Pin_9  
    #define MODBUS_USART_RX_PIN         GPIO_Pin_10  
    #define MODBUS_USART_IRQ            USART1_IRQn  
    #define MODBUS_USART_IRQHandler     USART1_IRQHandler  
#elif (USE_USART_NUM == 2)      // 使用串口2
    #define MODBUS_USART                USART2  
    #define MODBUS_USART_CLK            RCC_APB1Periph_USART2
    #define MODBUS_USART_CLK_CMD        RCC_APB1PeriphClockCmd
    #define MODBUS_USART_GPIO_CLK       RCC_APB2Periph_GPIOA
    #define MODBUS_USART_GPIO_CLK_CMD   RCC_APB2PeriphClockCmd
    #define MODBUS_USART_TX_PORT        GPIOA  
    #define MODBUS_USART_RX_PORT        GPIOA  
    #define MODBUS_USART_TX_PIN         GPIO_Pin_2  
    #define MODBUS_USART_RX_PIN         GPIO_Pin_3  
    #define MODBUS_USART_IRQ            USART2_IRQn  
    #define MODBUS_USART_IRQHandler     USART2_IRQHandler 
#else                           // 使用串口3
    #define MODBUS_USART                USART3
    #define MODBUS_USART_CLK            RCC_APB1Periph_USART3
    #define MODBUS_USART_CLK_CMD        RCC_APB1PeriphClockCmd
    #define MODBUS_USART_GPIO_CLK       RCC_APB2Periph_GPIOB
    #define MODBUS_USART_GPIO_CLK_CMD   RCC_APB2PeriphClockCmd
    #define MODBUS_USART_TX_PORT        GPIOB  
    #define MODBUS_USART_RX_PORT        GPIOB  
    #define MODBUS_USART_TX_PIN         GPIO_Pin_10  
    #define MODBUS_USART_RX_PIN         GPIO_Pin_11  
    #define MODBUS_USART_IRQ            USART3_IRQn  
    #define MODBUS_USART_IRQHandler     USART3_IRQHandler 
#endif


// Modbus定时器  
#define MODBUS_TIM                      TIM2        
#define MODBUS_TIM_CLK                  RCC_APB1Periph_TIM2  
#define MODBUS_TIM_CLK_CMD              RCC_APB1PeriphClockCmd  
#define MODBUS_TIM_IRQ                  TIM2_IRQn  
#define MODBUS_TIM_IRQHandler           TIM2_IRQHandler  

// 485宏定义
#define MODBUS_USE_RS485                TRUE          // 使用485该宏定义为 TRUE，否则定义为 FALSE

#if (MODBUS_USE_RS485 == TRUE)
#define MODBUS_RS485_EN_GPIO_CLK        RCC_APB2Periph_GPIOD
#define MODBUS_RS485_EN_GPIO_CLK_CMD    RCC_APB2PeriphClockCmd
#define MODBUS_RS485_EN_PORT            GPIOD  
#define MODBUS_RS485_EN_PIN             GPIO_Pin_1  
#endif


#endif
